<?php
namespace app\user\traits\models;

use think\Validate;
use Carbon\Carbon;

/**
 * 会员信息集中校验
 */
trait UserInfoCheck
{
    public $errors = '';
    public $datesa = '';
    protected $alias = [
        'username' => 'user_name', 
        'mail' => 'email', 
        'mobile' => 'mobile', 
        'password' => 'password', 
        'accepted' => 'accepted', 
        'repassword' => 'repassword',
    ];

    /**
     * [_initialize 初始化]
     * @return [type] [description]
     */
    // public function _initialize()
    // {
    //     session(['prefix' => '','auto_start' => true]);      
    // } 
    protected function getErrors($key = '')
    {
        if ($key) {
            return $this->errors[$key];
        }
        return $this->errors;
    }
    protected function clearErrors()
    {
        $this->errors = '';
    }
    /**
     * 校验数据
     * @param  array   $dates        待校验数据数组
     * @param  array   $onlyOrexcept 校验的字段
     * @param  boolean $limit        是否支持多错误提示
     * @return [type]                [description]
     */
    public function cheack($dates = [], $onlyOrexcept = [], $limit = false)
    {
        $this->datesa = $dates;
        $this->clearErrors();
        if (is_bool($onlyOrexcept)) {
            $limit = $onlyOrexcept;
        }else{
            if ($onlyOrexcept && is_array($onlyOrexcept)) {

                $onlyOrexcept_v = is_array($onlyOrexcept) ? $onlyOrexcept : explode(',', $onlyOrexcept);

                if (key($onlyOrexcept) == 'only') {
                    $dates = array_intersect_key($dates, array_flip(current($onlyOrexcept_v)));
                }elseif (key($onlyOrexcept) == 'except') {
                    $dates = array_diff_key($dates, array_flip(current($onlyOrexcept_v)));
                }
            }
        }
        foreach ($dates as $type => $value) {
            if (isset($this->alias[$type])) {
                $type = $this->alias[$type];
            }
            if (method_exists($this,$type)) {
                if(!$this->$type($value,$dates) && $limit == false){
                    return false;
                }
            }
        }
        return $this->getErrors() ? false : true;
    }
    /**
     * 校验用户email
     * @param  [type] $mail [description]
     * @return [type]       [description]
     */
    protected function email($email)
    {
        if (false == Validate::is($email, 'email')) {
            $this->errors['email'] = 'mail格式错误';
            return false;
        }
        return true;
    }
    /**
     * 校验用户名
     * @param  [type] $user_name [description]
     * @return [type]            [description]
     */
    protected function user_name($user_name)
    {
        if (false == Validate::is($user_name, 'require')) {
            $this->errors['user_name'] = 'require';
            return false;
        }else{
            if (false == Validate::length($user_name, '6,20')) {
                $this->errors['user_name'] = '长度不符';
                return false;
            }
        }
        return true;
    }
    /**
     * 校验用户手机格式
     * @param  [type] $mobile [description]
     * @return [type]         [description]
     */
    protected function mobile($mobile)
    {
        if(false == Validate::is($mobile, 'require')){
        	$this->errors['mobile'] = '手机号码不允许为空';
            return false;
        }elseif(false == Validate::regex($mobile,'^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$')) {
        	$this->errors['mobile'] = '手机号格式错误';
            return false;
        }
        return true;
        
    }
    /**
     * 校验用户密码
     * @param  [type] $password [description]
     * @return [type]           [description]
     */
    protected function password($password)
    {
        if (false == Validate::is($password, 'require')) {
            $this->errors['password'] = '密码不能为空';
            return false;
        }else{
            if (false == Validate::regex($password,'^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$')) {
                $this->errors['password'] = '密码应为6~16位的字母和数字组合';
                return false;
            }
        }
        return true;
    }
    /**
     * 重复密码
     * @param  [type] $repassword [description]
     * @param  [type] $d          [description]
     * @return [type]             [description]
     */
    protected function repassword($repassword)
    {
        if (false == Validate::is($repassword, 'require')) {
            $this->errors['repassword'] = '确认密码不能为空';
            return false;
        }else{
            if (false == Validate::confirm($repassword, 'password',$this->datesa)) {
                $this->errors['repassword'] = '两次输入的密码不一致，请重新输入';
                return false;
            }
        }
        return true;
    }
    /**
     * 是否同意协议
     * @param  [type] $accepted [description]
     * @return [type]           [description]
     */
    protected function accepted($accepted)
    {
        if (false == Validate::is($accepted, 'accepted')) {
            $this->errors['accepted'] = '请同意双辉旅程网服务协议';
            return false;
        }
        return true;
    }
}
